package com.yanqu.road.server.cmd.activity.pawnshop;

import java.util.List;
import java.util.Map;

import com.yanqu.road.entity.activity.pawnshop.*;
import com.yanqu.road.server.pb.PawnshopPb;
import com.yanqu.road.pb.activity.PawnshopProto;
import com.yanqu.road.pb.activity.PawnshopProto.*;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.pawnshop.PawnshopModule;
import com.yanqu.road.server.manger.activity.pawnshop.PawnshopMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_PAWNSHOP_DATA, desc = "请求当铺数据")
public class PawnshopDataCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (!PawnshopMgr.isActivityInTime()) {
            // 非活动时间内
            this.ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        PawnshopDataRespMsg.Builder builder = PawnshopDataRespMsg.newBuilder();
        builder.setRet(ret);
        if (ret == 0) {
            // 当铺数据
            builder.setDPawnshop(getDPawnshop(player));
            // 通知
            List<UserPawnshopNotify> notifyList = player.getModule(PawnshopModule.class).getUnReadNotifyList();
            builder.addAllDNotify(PawnshopPb.parseDPawnshopNotifyList(notifyList));
        }
        // 返回当铺配置
        player.sendPacket(ClientProtocol.U_PAWNSHOP_DATA, builder);
    }

    private DPawnshop.Builder getDPawnshop(GamePlayer player) {
        PawnshopModule psModule = player.getModule(PawnshopModule.class);
        UserPawnshop userPawnshop = psModule.getUserPawnshop();
        int levelPS = userPawnshop.getLevel();
        // 组装数据
        DPawnshop.Builder shopMsg = DPawnshop.newBuilder();
        shopMsg.setReceptionNum(userPawnshop.getReceptionNum());
        shopMsg.setLevel(levelPS);
        shopMsg.setCoinTotal(psModule.getTotalWealth());
        shopMsg.setCoinUsable(userPawnshop.getCoinUsable());
        shopMsg.setWashTimes(userPawnshop.getWashTimes());
        shopMsg.setStaminaUsable(userPawnshop.getStaminaUsable());
        shopMsg.setLastRestoreTime(userPawnshop.getLastRestoreTime());
        shopMsg.setIsReception(userPawnshop.isReception());
        shopMsg.setIcon(userPawnshop.getNpcIcon());
        shopMsg.setShopRewardLv(userPawnshop.getLvReward());
        Map<Integer, UserPawnshopStaff> staffMap = userPawnshop.getStaffMap();
        for (UserPawnshopStaff staff : staffMap.values()) {
            PawnshopProto.PawnshopStaffTempMsg.Builder staffMsg = PawnshopPb.parsePawnshopStaffTempMsg(staff);
            if (null != staffMsg) {
                shopMsg.addStaffList(staffMsg);
            }
        }
        // 物件数据
        for (long itemId : psModule.getItemIdList()) {
            UserPawnshopItem userItem = psModule.getUserPawnshopItem(itemId);
            PawnshopProto.DPawnshopItem.Builder itemMsg = PawnshopPb.parseDPawnshopItem(userItem);
            if(null != itemMsg) {
                shopMsg.addDItemList(itemMsg);
            }
        }
        // 时事数据
        PawnshopProto.DPawnshopPolitics.Builder pMsg = PawnshopPb.parseDPawnshopPolitics(userPawnshop);
        if(null != pMsg){
            shopMsg.setDPolitics(pMsg);
        }
        // 事件数据
        UserPawnshopEvent event = psModule.getUserPawnshopEvent();
        if (event != null) {
            shopMsg.setDEvent(PawnshopPb.getDPawnshopEvent(event));
        }
        return shopMsg;
    }




}
